﻿Public Class FrmTinhTrangXe
    Private selectedmatt As String


    Private Sub btnNhapLai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNhapLai.Click
        btnSua.Text = "&Sửa"
        txtTT.Text = ""
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Hide()
        Me.Close()
    End Sub

    Private Sub btnTimKiem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimKiem.Click
        btnSua.Text = "&Sửa"
        dgvTT.DataSource = TinhTrangXe.timkiem(txtTuKhoa.Text)
    End Sub

    Private Sub btndaydu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndaydu.Click
        btnSua.Text = "&Sửa"
        dgvTT.DataSource = TinhTrangXe.dstt()
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        btnSua.Text = "&Sửa"
        If txtTT.Text <> "" Then
            If Not TinhTrangXe.them(txtTT.Text) Then
                MsgBox("Bạn không thể thêm vào tình trạng này", MsgBoxStyle.Critical, "Lỗi")
            End If
            dgvTT.DataSource = TinhTrangXe.dstt()
            btnNhapLai_Click(sender, e)
        Else
            MsgBox("Bạn chưa nhập dữ liệu!", MsgBoxStyle.Information, "Thông báo")
        End If
    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click

        If btnSua.Text = "&Sửa" Then
            selectedmatt = dgvTT.CurrentRow.Cells("MaTT").Value
            txtTT.Text = dgvTT.CurrentRow.Cells("MoTa").Value
            btnSua.Text = "&Lưu"
        Else
            If Not TinhTrangXe.capnhat(selectedmatt, txtTT.Text) Then
                MsgBox("Bạn không thể lưu dữ liệu", MsgBoxStyle.Critical, "Lỗi")

            End If
            dgvTT.DataSource = TinhTrangXe.dstt()
            btnNhapLai_Click(sender, e)

        End If
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        btnSua.Text = "&Sửa"
        If MsgBox("Bạn chắc chắn muốn xóa tình trạng xe này chứ?", MsgBoxStyle.YesNo, "Thông báo") = MsgBoxResult.Yes Then
            If Not TinhTrangXe.xoa(dgvTT.CurrentRow.Cells("MaTT").Value) Then
                MsgBox("Bạn không thể xóa tình trạng xe này", MsgBoxStyle.Critical, "Lỗi")
            End If
            dgvTT.DataSource = TinhTrangXe.dstt()
            btnNhapLai_Click(sender, e)

        End If
    End Sub

    Private Sub dgvTT_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvTT.CellDoubleClick
        btnSua.Text = "&Sửa"
        btnSua_Click(sender, e)
    End Sub

    Private Sub FrmTinhTrangXe_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dgvTT.DataSource = TinhTrangXe.dstt()
    End Sub
End Class